package com.rabbit.videoplayer.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.rabbit.videoplayer.listener.OnVideoStatusListener
import com.rabbit.videoplayer.R
import com.rabbit.videoplayer.response.PlayerType
import kotlin.random.Random

class LoadStatusView(context: Context, attrs: AttributeSet?=null): ConstraintLayout(context, attrs), OnVideoStatusListener {

    private val pbVod: ProgressBar
    private val tvLoadPercentValue: TextView
    private val tvNetSpeed: TextView
    private val layoutLoadingProgressFull: ViewGroup
    private val layoutLoadingProgressSmall: ViewGroup

    private var playerType = -1

    init {
        LayoutInflater.from(context).inflate(R.layout.view_wb_mix_vod_load_status, this, true)
        tvNetSpeed = findViewById(R.id.tv_video_net_speed)
        tvLoadPercentValue = findViewById(R.id.tv_video_load_percent_value)
        pbVod = findViewById(R.id.loading_pbar)
        layoutLoadingProgressFull = findViewById(R.id.layout_loading_progress_big)
        layoutLoadingProgressSmall = findViewById(R.id.layout_loading_progress_small)

        hide()
    }


    /**
     * 更新加载进度
     * @param percent 百分比
     */
    private fun updateLoadingPercent(percent: Int, netSpeed: Float) {
        if (tvLoadPercentValue.visibility != View.VISIBLE) {
            tvLoadPercentValue.visibility = View.VISIBLE
        }
        tvLoadPercentValue.text = "$percent%"

        // 获取当前的网络下行码率，数据类型为Float，单位为bps。
        if (tvNetSpeed.visibility != View.VISIBLE) {
            tvNetSpeed.visibility = View.VISIBLE
        }

        tvNetSpeed.text = byteToMB(netSpeed)
    }

    //将字节数转化为MB
    private fun byteToMB(size: Float): String {
        // 1B = 8b，1000是为了避免出现1023KB/s情况
        val kb: Long = 1000 * 8
        val mb = kb * 1024
        val gb = mb * 1024
        return if (size < kb) {
            String.format("%dB/s", size.toLong())
        } else if (size < mb){
            val f = size / kb
            String.format(if (f > 100) "%.0fKB/s" else "%.1fKB/s", f)
        } else if (size < gb) {
            val f = size / mb
            String.format(if (f > 100) "%.0fMB/s" else "%.1fMB/s", f)
        } else {
            String.format("%.1fGB/s", size / gb)
        }
    }


    fun hide(){
        this.visibility = View.GONE

        layoutLoadingProgressSmall.visibility = View.GONE
        layoutLoadingProgressFull.visibility = View.GONE

        val randomPercent = Random.nextInt(from = 1, 7)
        tvLoadPercentValue.text = "$randomPercent%"

        val randomSpeed = Random.nextInt(from = 10, 25)
        tvNetSpeed.text = String.format("%dKB/s", randomSpeed)
    }

    fun showLoadingStatus(){
        layoutLoadingProgressFull.visibility = View.GONE
        layoutLoadingProgressSmall.visibility = View.VISIBLE
        this.visibility = View.VISIBLE
    }

    override fun onVodInit(vararg init: Any?): Boolean {
        if (init.isNotEmpty()) {
            playerType = init[0] as Int
        }
        showLoadingStatus()
        return false
    }

    override fun onVodPrepared(vararg prepared: Any) { // 数据解析完成，但不一定是加载完成
        showLoadingStatus()
    }
    override fun onVodFirstFrameStart() { // 显示第一帧，但可能还在持续加载中，因此此时不能隐藏加载状态
//        hide()
    }
    override fun onVodStart() {
        hide()
    }


    override fun onVodSwitchDisplayControlView() {

    }

    override fun onVodPause() {

    }

    override fun onVodStop() {
        hide()
    }

    override fun onVodError(vararg params: Any) {
        hide()
    }

    override fun onVodComplete() {
        hide()
    }

    override fun onVodRelease() {
        hide()
    }

    override fun onVodLockedScreen(vararg params: Any) {

    }

    override fun onVodFullScreen(vararg params: Any) {

    }

    override fun onVodLoadingBegin() {
        if (playerType == PlayerType.ALIYUN) {
            layoutLoadingProgressFull.visibility = View.VISIBLE
            layoutLoadingProgressSmall.visibility = View.GONE
        } else {
            layoutLoadingProgressFull.visibility = View.GONE
            layoutLoadingProgressSmall.visibility = View.VISIBLE
        }
        this.visibility = View.VISIBLE
    }

    override fun onVodLoadingProgress(vararg params: Any) {
        if (params.isEmpty()) return
        val percent = params[0] as Int
        val netSpeed = params[1] as Long
        if (playerType != PlayerType.ALIYUN) { // 只有ali播放器才能显示进度
            return
        }
        updateLoadingPercent(percent, netSpeed.toFloat())
    }

    override fun onVodLoadingEnd() {
        hide()
    }

    override fun onVodBackPress(): Boolean {
        return false
    }

    override fun onEpisodeSelected() {
    }

    override fun onVodShowUnlockUI(isDisplayUI: Boolean) {
        if (isDisplayUI) {
            hide()
        } else {
            showLoadingStatus()
        }
    }

    override fun onVodSkits() {

    }
}